/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package inet.dao;

import inet.entity.Notes;
import inet.util.Md5;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import inet.ulti.DateTimeUlti;

/**
 *
 * @author ÂuNgọcThái
 */
public class NotesDAO extends BaseDAO {
// hiển thi 1 note

    public Notes showNotes(BigDecimal empid) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = null;
        try {
            sql = "SELECT NOTE_ID, EMP_ID, CONTENT, NOTE_DATE, LAST_UPDATE FROM NOTES WHERE EMP_ID = ? and NOTE_DATE=?";
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            ps.setBigDecimal(1, empid);
            ps.setString(2, DateTimeUlti.noteDate());
            rs = ps.executeQuery();
            Notes note = null;
            if (rs.next()) {
                note = new Notes();
                int i = 1;
                note.setNote_id(rs.getBigDecimal(i++));
                note.setEmp_id(rs.getBigDecimal(i++));
                note.setContent(rs.getString(i++));
                note.setNote_date(rs.getString(i++));
                note.setLast_update(rs.getTimestamp(i++));

            }
            return note;
        } catch (Exception ex) {
            System.out.println("Error in sql: " + sql);
            ex.printStackTrace();
        } finally {
            releaseConnection(conn, ps, rs);
        }
        return null;
    }

    // thêm note mới
    public boolean addNotes(BigDecimal empid) {
        Connection conn = null;
        PreparedStatement ps = null;
        String sql = null;
        boolean stt = false;
        try {
            sql = "INSERT INTO NOTES(NOTE_ID,EMP_ID,CONTENT,NOTE_DATE,LAST_UPDATE) VALUES (NOTES_SEQ.NEXTVAL,?,null,?,CURRENT_TIMESTAMP)";
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            ps.setBigDecimal(1, empid);
            ps.setString(2, DateTimeUlti.noteDate());
            int i = ps.executeUpdate();
            if (i == 1) {
                stt = true;
            } else {
                stt = false;
            }

        } catch (Exception ex) {
            System.out.println("Error in sql: " + sql);
            ex.printStackTrace();
        } finally {
            releaseConnection(conn, ps);
        }
        return stt;
    }

    //cập nhật note
    public boolean updateNotes(String cont, BigDecimal id) {
        Connection conn = null;
        PreparedStatement ps = null;
        String sql = null;
        boolean stt = false;
        try {
            sql = "UPDATE NOTES SET CONTENT=?,LAST_UPDATE=CURRENT_TIMESTAMP WHERE NOTE_ID=?";
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            ps.setString(1, cont);
            ps.setBigDecimal(2, id);
            int i = ps.executeUpdate();
            if (i == 1) {
                stt = true;
            } else {
                stt = false;
            }

        } catch (Exception ex) {
            System.out.println("Error in sql: " + sql);
            ex.printStackTrace();
        } finally {
            releaseConnection(conn, ps);
        }
        return stt;
    }

    //show note
    public Notes showNoteByID(BigDecimal id) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = null;
        try {
            sql = "SELECT NOTE_ID, EMP_ID, CONTENT, NOTE_DATE, LAST_UPDATE FROM NOTES WHERE NOTE_ID = ?";
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            ps.setBigDecimal(1, id);
            rs = ps.executeQuery();
            Notes note = null;
            if (rs.next()) {
                note = new Notes();
                int i = 1;
                note.setNote_id(rs.getBigDecimal(i++));
                note.setEmp_id(rs.getBigDecimal(i++));
                note.setContent(rs.getString(i++));
                note.setNote_date(rs.getString(i++));
                note.setLast_update(rs.getTimestamp(i++));

            }
            return note;
        } catch (Exception ex) {
            System.out.println("Error in sql: " + sql);
            ex.printStackTrace();
        } finally {
            releaseConnection(conn, ps, rs);
        }
        return null;

    }
//    public static void main(String[] a) {
//        DateFormat dateFormat = new SimpleDateFormat("MM/yyyy");
//        Date date = new Date();

//       if( d.addNotes(BigDecimal.valueOf(2), "3/2015")){
//           System.out.print("ok");
//       }else{
//           System.out.print("loi");
//       }
//        Notes e = new Notes();
//
//        e = d.showNotes(BigDecimal.valueOf(2), dateFormat.format(date));
//        if (e == null) {
//            System.out.print("loi");
//
//        } else {
//            //System.out.print("ok");
//            System.out.println(e.getNote_id());
//            System.out.println(e.getEmp_id());
//            System.out.println(e.getContent());
//            System.out.println(e.getLast_update());
//            System.out.println(e.getNote_date());
//
//        }
//        
//        NotesDAO d = new NotesDAO();
//         if( d.updateNotes("Thái", BigDecimal.valueOf(3))){
//           System.out.print("ok");
//       }else{
//           System.out.print("loi");
//       }
//    }
}
